from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from contractsign.models import SoftContract
from .models import SoftAdContent

@login_required
def editor_dashboard(request):
    if request.user.role != 'editor':
        return redirect('/')
    editable_contracts = SoftContract.objects.exclude(
        id__in=SoftAdContent.objects.values_list('contract_id', flat=True)
    )
    print(editable_contracts)
    my_contents = SoftAdContent.objects.filter(editor=request.user).order_by('-id')
    return render(request, 'editor_dashboard.html', {
        'editable_contracts': editable_contracts,
        'my_contents': my_contents
    })

@login_required
def edit_soft_contract(request, contract_id):
    if request.user.role != 'editor':
        return redirect('/')

    contract = get_object_or_404(SoftContract, id=contract_id)

    if request.method == 'POST':
        SoftAdContent.objects.create(
            contract=contract,
            editor=request.user,
            content=request.POST['content'],
            image_path=request.POST.get('image_path', ''),
            video_path=request.POST.get('video_path', ''),
            status='pending'
        )
        return redirect('editor_dashboard')

    return render(request, 'edit_soft_contract.html', {'contract': contract})
